package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Process;
import com.example.yk2021xm04.query.ProcessQuery;
import com.example.yk2021xm04.service.IProcessService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZengQi
 * @since 2022-01-20
 */
@Controller
@RequestMapping("/process")
public class ProcessController {
    @Resource
    private IProcessService processService;

    @RequestMapping("index")
    public String index() {return "process/process";}

    @RequestMapping("addOrUpdateProcessPage")
    public String addOrUpdateProcessPage(Integer id, Model model)
    {
        if(null!=id){
            model.addAttribute("process",processService.getById(id));
        }
        return "process/add_update";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> processList(ProcessQuery processQuery)
    {
        return processService.processList(processQuery);
    }
    
    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleleProcess(Integer[] ids){
        processService.deleteProcess(ids);
        return RespBean.success("进度记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addProcess(Process process) {
        processService.addProcess(process);
        return RespBean.success("合同记录添加成功");
    }

    @RequestMapping("update")
    @ResponseBody
    public RespBean updateProcess(Process process) {
        processService.updateProcess(process);
        return RespBean.success("合同记录更新成功");
    }

    @RequestMapping("count")
    public String statistic() {
        return "process/process-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean processStatistic() {
        //发布时间分组 group by day
        List<Object> registerTimeByDay = processService.uploadTimeByDay();
        //一天中的发布任务数
        List<Object> registerCountByDay = processService.uploadCountByDay();
        //日同比
        double rate = 0.0;
        //总任务数量
        Integer totalProcessCount = processService.count();
        //今天的发布任务数量
        Integer ProcessCountInDay = processService.ProcessCountInDay();
        //昨天的发布任务数量
        Integer ProcessCountInYesterday = processService.ProcessCountInYesterday();

        rate = (double)(ProcessCountInDay - ProcessCountInYesterday) / ProcessCountInYesterday;
        if(ProcessCountInYesterday == 0) {
            rate = 0;
        }
        Map<String, Object> res = new HashMap<>();
        res.put("num", registerCountByDay);
        res.put("time", registerTimeByDay);
        res.put("total", totalProcessCount);
        res.put("dayCount", ProcessCountInDay);
        res.put("rate", rate);
        return RespBean.success("任务统计数据返回成功",res);
    }
}
